#include "SkySphereBackground.h"

using namespace OBR;

SkySphereBackground::SkySphereBackground() : Background("shaders/Background/SkySphereBackground.cu")
{
    this->program_desc.miss.entryFunctionName = "__miss__sky_sphere_background";
    this->program_desc.kind = OPTIX_PROGRAM_GROUP_KIND_MISS;
    this->program_desc.flags = OPTIX_GEOMETRY_FLAG_NONE;
}

SkySphereBackground::~SkySphereBackground()
{
}

void SkySphereBackground::uploadSbtData()
{
    SkySphereBackgroundData data;
    data.bg = this->texture.getDeviceTex2D();

    this->sbt_data.resize(sizeof(data));
    this->sbt_data.upload(&data, sizeof(data));
}

void SkySphereBackground::_buildFromConfig(const Json::Value &config)
{
    if (config["type"].asString().compare("sphere") != 0)
    {
        std::cerr << "wrong config value is sent to SkySphereBackground::_buildFromConfig"
                  << std::endl;
        std::cerr << "got:" << config << std::endl;
        throw std::runtime_error("invalid parameter");
    }
    ConfigHelpers::loadTexture2D<Spectrum>(config["texture"], this->texture);
}

OptixProgramGroupOptions &SkySphereBackground::getProgramGroupOptions()
{
    return this->program_opt;
}

OptixProgramGroupDesc &SkySphereBackground::getProgramGroupDesc()
{
    this->program_desc.miss.module = this->cuda_module->optix_module;
    return this->program_desc;
}